<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="上传类型" prop="remarks" :rules="[
                 ]">
              <el-select v-model="inputForm.remarks" placeholder="请选择上传类型">
                <el-option label="图片" value="1"></el-option>
                <el-option label="视频" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型" prop="businessType" :rules="[
                  {required: true, message:'业务类型不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.businessType" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in $dictUtils.getDictList('business_type')" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="this.sysNo==1">
            <el-form-item v-if="inputForm.businessType==1" label="店铺ID" prop="shop.id" :rules="[
                 ]">
              <GridSelect title="选择店铺ID" labelName='shopName' labelValue='id' :value="inputForm.shop.id" :limit="1"
                @getValue='(value) => {inputForm.shop.id=value}' :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'shopName',
              label: '门店名称'
            },
            {
              prop: 'shopIntroduce',
              label: '门店介绍'
            },
            {
              prop: 'contacts',
              label: '联系人'
            },
            {
              prop: 'contactsPhone',
              label: '联系人电话'
            }
            ]" :searchs="[
            {
              prop: 'shopName',
              label: '门店名称'
            }
            ]" dataListUrl="/tshopinfo/tShopInfo/list" entityBeanName="tShopInfo"
                queryEntityUrl="/tshopinfo/tShopInfo/queryById">
              </GridSelect>
            </el-form-item>
            <el-form-item v-else label="店铺ID" prop="shop.id" :rules="[
                 ]">
              <GridSelect title="选择店铺" labelName='storeName' labelValue='id' :value="inputForm.shop.id" :limit="1"
          size="small" @getValue='(value) => {inputForm.shop.id=value}' :columns="[
                    {
                      prop: 'id',
                      label: '主键'
                    },
                    {
                      prop: 'storeName',
                      label: '门店名称'
                    },
                    {
                      prop: 'storeIntroduction',
                      label: '门店介绍'
                    },
                    {
                      prop: 'legalName',
                      label: '联系人'
                    },
                    {
                      prop: 'contactPhoneNumber',
                      label: '联系人电话'
                    }
                    ]" :searchs="[
                    {
                      prop: 'storeName',
                      label: '门店名称'
                    }
                    ]" dataListUrl="/distributor/vDistributor/list" entityBeanName="vDistributor"
          queryEntityUrl="/distributor/vDistributor/queryById">
        </GridSelect>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="inputForm.remarks==1">
            <el-form-item label="标题" prop="title" :rules="[
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写标题"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="文件" prop="image" :rules="[
                 ]">

              <el-upload class="upload-demo" ref="image"
                :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/articleinfo/tArticleInfo`"
                :headers="{token: $cookie.get('token')}" :on-preview="(file, fileList) => {
                      showImg(file.url)
                    }" :on-success="(response, file, fileList) => {
                       inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }" :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.image = fileList.map(item => item.url).join('|')
                    }" :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }" multiple :limit="1" :on-exceed="(files, fileList) =>{
                  $message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                }" :file-list="imageArra">
                <el-button size="small" type="primary">点击上传</el-button>
                <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
              </el-upload>
              <video v-if="inputForm.remarks==2" scrolling="no" autoplay style="scrolling:no;background-color: black;"
                height="100px" width="100px" :src="inputForm.image" controls="controls">
              </video>
              <el-image v-if="inputForm.remarks==1" style="width: 100px; height: 100px" :src="inputForm.image"
                @click="showImg(inputForm.image)">
              </el-image>
              <!-- <el-upload ref="image"  
              v-if="visible && inputForm.remarks==1"
              list-type="picture-card"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/articleinfo/tArticleInfo`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {
                      showImg(file.url)
                    }"
                    :on-success="(response, file, fileList) => {
                       inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.image = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="imageArra">
                    <i class="el-icon-plus"></i>
                  </el-upload> -->
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="inputForm.remarks==1">
            <el-form-item label="内容" prop="content" :rules="[
                 ]">
              <WangEditor ref="contentEditor" v-model="inputForm.content" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="inputForm.auditStatus == '0'" @click="audit(inputForm.id,1)">通过</el-button>
        <el-button v-if="inputForm.auditStatus == '0'" @click="jj(inputForm.id,2)">拒绝</el-button>
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data() {
      return {
        title: '',
        method: '',
        sysNo: localStorage.getItem('sysNo'),
        visible: false,
        loading: false,
        imageArra: [],
        inputForm: {
          id: '',
          shop: {
            id: ''
          },
          title: '',
          image: '',
          content: '',
          remarks: '1',
          auditStatus: ''
        }
      }
    },
    components: {
      GridSelect,
      WangEditor
    },
    methods: {
      showImg(url) {
        this.$hevueImgPreview(url)
      },
      init(method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建`
        } else if (method === 'edit') {
          this.title = '修改'
        } else if (method === 'view') {
          this.title = '查看'
        }
        this.imageArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.contentEditor.init('')
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/articleinfo/tArticleInfo/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({ data }) => {
              this.inputForm = this.recover(this.inputForm, data.tArticleInfo)
              this.$refs.contentEditor.init(this.inputForm.content)
              this.inputForm.image.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.imageArra.push({ name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item })
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit() {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/articleinfo/tArticleInfo/save`,
              method: 'post',
              data: this.inputForm
            }).then(({ data }) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      },
      audit(id, state) {
        this.loading = true
        this.$http({
          url: '/articleinfo/tArticleInfo/save',
          method: 'post',
          params: {
            'id': id,
            "auditStatus": state
          }
        }).then(({ data }) => {
          if (data && data.success) {
            this.inputForm.auditStatus = state
            this.$message.success(data.msg)
            this.$parent.refreshList()
          }
          this.loading = false
        })
      },
      jj(id, state) {
        this.$prompt('请输入拒绝原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          if (!value) {
            this.$message({
              type: 'info',
              message: '请输入拒绝原因'
            });
            return false
          }
          this.loading = true
          this.$http({
            url: '/articleinfo/tArticleInfo/save',
            method: 'post',
            params: {
              'id': id,
              "auditStatus": state,
              'reason': value
            }
          }).then(({ data }) => {
            if (data && data.success) {
              this.inputForm.auditStatus = state
              this.$message.success(data.msg)
              this.$parent.refreshList()
            }
            this.loading = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });
      },
    }
  }
</script>